package com.bst.bsj.portal.util;

import java.math.BigDecimal;

import com.homewaimai.common.lang.StringUtil;

public class WebUtil {

	private static WebUtil instance = new WebUtil();

	public static WebUtil getInstatnce() {
		return instance;
	}

	public String isNotBlank(String content) {
		if (StringUtil.isNotBlank(content)) {
			return "Y";
		}
		return "N";
	}

	/**
	 * 字符串截断
	 * 
	 * @param content
	 * @param len
	 * @param post
	 * @return
	 */
	public String left(String content, int len, String post) {
		if (StringUtil.isNotBlank(content) && content.length() > len) {
			return StringUtil.left(content, len) + post;
		} else {
			return content;
		}

	}

	/**
	 * 将分转换为元和小数点标识的人民币
	 * 
	 * @param price
	 * @return
	 */
	public String getPrice(BigDecimal price) {
		if (null != price) {
			return new java.text.DecimalFormat("0.00")
					.format(price.longValue() / 100.0);
		}
		return "";
	}

	/**
	 * @param price
	 * @return
	 */
	public String getPrice(BigDecimal price, int num) {

		String tmp = "0";

		if (num > 0) {

			tmp = "0.";
			for (int i = 0; i < num; ++i) {
				tmp = tmp + "0";
			}

		}

		if (null != price) {
			return new java.text.DecimalFormat(tmp)
					.format(price.longValue() / 100.0);
		}
		return "";
	}

	/**
	 * 将分转换为元和小数点标识的人民币
	 * 
	 * @param priceStr
	 *            价格字符串
	 * @return
	 */
	public String getPrice(String priceStr) {

		BigDecimal price = new BigDecimal(priceStr);

		if (null != price) {
			return new java.text.DecimalFormat("0.00")
					.format(price.longValue() / 100.0);
		}
		return "";
	}

	/**
	 * 
	 * @param priceStr
	 * @param num
	 * @return
	 */
	public String getPrice(String priceStr, int num) {

		String tmp = "0";

		if (num > 0) {

			tmp = "0.";
			for (int i = 0; i < num; ++i) {
				tmp = tmp + "0";
			}

		}

		BigDecimal price = new BigDecimal(priceStr);

		if (null != price) {
			return new java.text.DecimalFormat(tmp)
					.format(price.longValue() / 100.0);
		}
		return "";
	}

	public String getFirstImg(String imgs) {
		if (StringUtil.isNotBlank(imgs)) {
			String[] imgList = imgs.split(";");
			if (imgList.length > 0) {
				return imgList[0];
			}
		}
		return null;
	}

	
	public String getFirstImg(String imgs, String split) {
        if (StringUtil.isNotBlank(imgs)) {
            String[] imgList = imgs.split(split);
            if (imgList.length > 0) {
                return imgList[0];
            }
        }
        return null;
    }
	
	
	public String getThumbImg(String sourceImg, String thumbImgs) {
		String targetImg = sourceImg;
		if (StringUtil.isNotBlank(sourceImg)) {
			if (!thumbImgs.startsWith("!")) {
				thumbImgs = "!" + thumbImgs;
			}
			int pos = sourceImg.indexOf("!");
			if (pos != -1) {
				targetImg = sourceImg.substring(0, pos) + thumbImgs;
			} else {
				targetImg = sourceImg + thumbImgs;
			}
		}
		return targetImg;
	}

	public boolean contains(String content, String keyword) {
		if (StringUtil.isNotBlank(content)) {
			return content.contains(keyword);
		}
		return false;
	}

}
